Dart RegExp operator ==
Syntax & Examples


RegExp.operator == operator

The equality operator `==` with Dart's `RegExp` objects as operands compares the two RegExp objects for equality.


Syntax of RegExp.operator ==

The syntax of RegExp.operator == operator is:

operator ==(dynamic other) → bool

This operator == operator of RegExp the equality operator.

Parameters

ParameterOptional/RequiredDescription
otherrequiredThe object to compare for equality with this object.


✐ Examples

1 Equality of Two Identical RegExp Objects

In this example,

  1. We create two identical regular expressions `regex1` and `regex2` with the pattern 'hello'.
  2. We compare them using the equality operator `==`.

Dart Program

void main() {
  RegExp regex1 = RegExp('hello');
  RegExp regex2 = RegExp('hello');
  print('regex1 == regex2: ${regex1 == regex2}');
}

Output

regex1 == regex2: true

2 Inequality of Two Different RegExp Objects

In this example,

  1. We create two different regular expressions `regex1` and `regex2` with different patterns.
  2. We compare them using the equality operator `==`.

Dart Program

void main() {
  RegExp regex1 = RegExp('hello');
  RegExp regex2 = RegExp('world');
  print('regex1 == regex2: ${regex1 == regex2}');
}

Output

regex1 == regex2: false

3 Equality of Two Identical RegExp Objects with Complex Pattern

In this example,

  1. We create two identical regular expressions `regex1` and `regex2` with the pattern '\d+'.
  2. We compare them using the equality operator `==`.

Dart Program

void main() {
  RegExp regex1 = RegExp(r'\d+');
  RegExp regex2 = RegExp(r'\d+');
  print('regex1 == regex2: ${regex1 == regex2}');
}

Output

regex1 == regex2: true

Summary

In this Dart tutorial, we learned about operator == operator of RegExp: the syntax and few working examples with output and detailed explanation for each example.